package eerbaar.internal.level;

import eerbaar.context.IContext;
import eerbaar.level.IActions;
import eerbaar.level.IExpressions;
import eerbaar.level.IState;
import eerbaar.shared.message.DebugMsg;
import eerbaar.shared.message.MsgHelper;

public class State implements IState {

    private IActions actions = new ActionCollection();

    private IExpressions expressions = new Expressions();

    private IContext model;

    private int priority;

    @Override
    public boolean check() {

        boolean result = this.expressions.execute(this.model);
        if (result) {

            MsgHelper.sendToDefaultReaders(new DebugMsg("State.executing"));

            assert (this.model != null);
            this.actions.execute(this.model);
        }

        return result;
    }

    @Override
    public IActions getActions() {
        return this.actions;
    }

    @Override
    public IExpressions getExpressions() {
        return this.expressions;
    }

    @Override
    public int getPriority() {
        return this.priority;
    }

    @Override
    public void setActions(IActions actions) {
        this.actions = actions;
    }

    @Override
    public void setExpressions(IExpressions expressions) {
        this.expressions = expressions;
    }

    @Override
    public void setPriority(int priority) {
        this.priority = priority;
    }

    @Override
    public void start(IContext model) {
        this.model = model;

        MsgHelper.sendToDefaultReaders(new DebugMsg("State.start: "
                + this.priority));

        this.expressions.start();
    }

    @Override
    public void stop() {

        MsgHelper.sendToDefaultReaders(new DebugMsg("State.stop: "
                + this.priority));

        this.expressions.stop();
        this.model = null;
    }

}
